package com.neolix.watch.service;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.neolix.watch.bluetooth.BlueToothUtil;
import com.neolix.watch.domain.NotificationType;
import com.neolix.watch.util.CommonHelper;
import com.neolix.watch.util.SharedPreferencesUtil;

import static com.neolix.watch.util.StringUtil.isNotBlank;
import static java.lang.String.format;


public class WatchInfoListener extends NotificationListenerService {

    private String TAG = WatchInfoListener.class.getSimpleName();

    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Notification mNotification = sbn.getNotification();
        Log.i(TAG, sbn.getPackageName() + "text" + mNotification.tickerText);
        if (mNotification != null) {
            String notifiation;
            if (sbn.getPackageName().equals("com.tencent.mm")) {
                notifiation = mNotification.tickerText.toString();
                if (SharedPreferencesUtil.isSendWXInfo()) {
                    //TODO 发送微信消息到手表
                    BlueToothUtil.syncNotification(NotificationType.Wx, "", notifiation);
                    Log.i(TAG, sbn.getPackageName() + "text" + mNotification.tickerText);
                }
            } else if (sbn.getPackageName().equals("com.android.phone")) {
                //TODO 发送微信消息到手表 //小米手机现在无法接听电话广播先才能此方法
                notifiation = mNotification.tickerText.toString();
                if (!Build.MANUFACTURER.contains("Xiaomi"))
                    return;

                if (SharedPreferencesUtil.isSendInComingInfo()) {
                    if (notifiation.contains("未接"))
                        return;

                    String owner = notifiation.replace(" ", "");
                    String number = CommonHelper.getContactNameByPhoneNumber(getApplication(), owner);
                    BlueToothUtil.syncNotification(NotificationType.TELPHONE, isNotBlank(number) ? number : owner, "");
                }
            }
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
    }
}